﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MovieApp
{
    public partial class SettingsWindow : Form
    {
        SettingsHandler setttings;
        MainWindow mainWindow;

        public SettingsWindow(MainWindow mw)
        {
            InitializeComponent();
            mainWindow = mw;

            setttings = new SettingsHandler();
            TbMovieFolder.Text = setttings.MoviePath;
            DateTime now = DateTime.Now;
            timePicker.Value = new DateTime(now.Year, now.Month, now.Day, 0, setttings.AutoSaveMinutes, setttings.AutoSaveSeconds);
            
            chkAutoSave.Checked = setttings.AutoSave;
            chkSaveOnExit.Checked = setttings.SaveOnExit;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            //validate SaveInterval
            if (setttings.AutoSave && setttings.AutoSaveIntervalMs < 60000)
            {
                MessageBox.Show("The Auto Save Interval must be greater or equal to one minute.", "Error");
                return;
            }
            //save settings
            setttings.Save();
            this.Hide();

            //refresh treeView
            mainWindow.InitWindow();
        }

        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                TbMovieFolder.Text = fbd.SelectedPath;
                setttings.MoviePath = TbMovieFolder.Text;
            }
        }

        private void chkAutoSave_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoSave.Checked == true)
                timePicker.Enabled = true;
            else
                timePicker.Enabled = false;

            setttings.AutoSave = chkAutoSave.Checked;
        }

        private void timePicker_ValueChanged(object sender, EventArgs e)
        {
            setttings.AutoSaveInterval = timePicker.Text;
        }

        private void chkSaveOnExit_CheckedChanged(object sender, EventArgs e)
        {
            setttings.SaveOnExit = chkSaveOnExit.Checked;
        }
    }
}
